package com.checknull.druineage;

import com.alibaba.druid.util.StringUtils;
import com.checknull.druineage.analysis.Lineage;
import com.checknull.druineage.element.Column;
import com.checknull.druineage.element.DB;
import com.checknull.druineage.element.Schema;
import com.checknull.druineage.element.Table;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class Demo {

    public static class Result {

        private String tabCol;
        private boolean colIsVal;
        private boolean colIsUnKnown;
        private List<Result> src = new ArrayList<>();

        public Result(Column column) {
            if(column.getOwner()==null){
                tabCol=column.getName();
            }else{
                Table table = column.getOwner();
                Schema schema = table.getOwner();
                tabCol = (StringUtils.isEmpty(schema.getName()) ? "" : schema.getName() + ".") + table.getName() + "." + column.getName();
            }
            colIsVal=column.isVal();
            colIsUnKnown=column.isUnknown();
            for (Column srcCol : column.getSrcColumns()) {
                src.add(new Result(srcCol));
            }
        }

        public String getTabCol() {
            return tabCol;
        }

        public void setTabCol(String tabCol) {
            this.tabCol = tabCol;
        }

        public boolean isColIsVal() {
            return colIsVal;
        }

        public void setColIsVal(boolean colIsVal) {
            this.colIsVal = colIsVal;
        }

        public boolean isColIsUnKnown() {
            return colIsUnKnown;
        }

        public void setColIsUnKnown(boolean colIsUnKnown) {
            this.colIsUnKnown = colIsUnKnown;
        }

        public List<Result> getSrc() {
            return src;
        }

        public void setSrc(List<Result> src) {
            this.src = src;
        }
    }

    @Test
    public void testSubUnion() throws JsonProcessingException {
        String sql = "insert into new_table\n" +
                "select t.a, t.b\n" +
                "from ((select b, a, c from src_table2) union all (select c, b, a from src_table3)) t";
        Lineage lineage = new Lineage("oralce");
        DB db = lineage.analysis(sql);
        ObjectMapper objectMapper = new ObjectMapper();
        for (Schema schema : db.getSchemas()) {
            for (Table table : schema.getTables()) {
                for (Column column : table.getColumns()) {
                    System.out.println(objectMapper.writeValueAsString(new Result(column)));
                }
            }
        }
    }

    @Test
    public void testUsedAgain() throws JsonProcessingException {
        String sql = "insert into new_table\n" +
                "select t.a,\n" +
                "       t.b,\n" +
                "       t3.c,\n" +
                "       t3.e,\n" +
                "       t4.f,\n" +
                "       t4.g,\n" +
                "       t7.x,\n" +
                "       t7.y,\n" +
                "       t7.z\n" +
                "from src_table t\n" +
                "         left join (select t2.*, t6.e\n" +
                "                    from src_table t2\n" +
                "                             left join src_table3 t6 on t2.i = t6.d\n" +
                "                    union all\n" +
                "                    select *\n" +
                "                    from src_table2 t5) t3 on t.e = t3.f\n" +
                "         left join src_table t4 on t.h = t4.h\n" +
                "         left join (select * from (select case when x = 3 then 3 else y end x, y, z from src_table) t8) t7\n" +
                "                   on t3.d = t7.z";
        DB db = new Lineage("oralce")
                .analysis(sql);
        ObjectMapper objectMapper = new ObjectMapper();
        for (Schema schema : db.getSchemas()) {
            for (Table table : schema.getTables()) {
                for (Column column : table.getColumns()) {
                    System.out.println(objectMapper.writeValueAsString(new Result(column)));
                }
            }
        }
    }
}